package com.rdxer.api;

import com.rdxer.api.para.IdsStringPara;
import com.rdxer.core.entity.Result;
import com.rdxer.core.entity.StatusCode;
import com.rdxer.core.utils.search.SearchPara;
import com.rdxer.model.Notice;
import com.rdxer.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/noticeManagement")
public class NoticeManagementApi {
    @Autowired
    NoticeService service;


    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @GetMapping("/show/{id}")
    public Result<Notice> findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", service.findById(id));
    }

    @PreAuthorize("@cks.permission('notice_management')")
    /**
     * @param model
     */
    @PostMapping("/doEdit")
    public Result doEdit(@RequestBody Notice model) {

        Result<Notice> result = new Result<>();
        result.setData(service.save(model));
        result.setMessage("操作成功");
        return result;
    }

    @PreAuthorize("@cks.permission('notice_management')")
    /**
     * @param model
     */
    @PostMapping("/doAdd")
    public Result add(@RequestBody Notice model) {
        Result<Notice> result = new Result<>();
        result.setData(service.add(model));
        result.setMessage("新增成功");
        return result;
    }

    @PreAuthorize("@cks.permission('notice_management')")
    /**
     */
    @PutMapping("/{id}")
    public Result<Notice> update(@PathVariable String id, @RequestBody Notice model) {

        model.setId(id);

        Notice data = service.update(model);

        Result<Notice> result = new Result<>();
        result.setData(data);
        result.setMessage("修改成功");
        return result;
    }

    @PreAuthorize("@cks.permission('notice_management')")
    /**
     * 删除
     */
    @PostMapping("/doDelete")
    public Result<?> delete(@RequestBody IdsStringPara ids) {


        service.deleteByIds(ids.getStrIds());
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PreAuthorize("@cks.permission('notice_management')")
    //	 * 分页+多条件查询
    @PostMapping("/getList")
    public Result<Page<Notice>> search(@Valid @RequestBody SearchPara para) {
        Result<Page<Notice>> result = new Result<>();
        Page<Notice> data = service.search(para);
        result.setData(data);
        return result;
    }

    //	 * 不分页+多条件查询
    @PostMapping("/getAll")
    public Result<List<Notice>> searchNoPage(@Valid @RequestBody SearchPara para) {
        Result<List<Notice>> result = new Result<>();
        List<Notice> data = service.searchNoPage(para);
        result.setData(data);
        return result;
    }
}
